﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SqlCon;

namespace yfxt
{
    public partial class gl_ypsl : Form
    {
        public gl_ypsl()
        {
            InitializeComponent();
        }

        string comStr;
        public string lingyaodanhao;
        int yksb;

        #region 刷新函数              ---显示左和右DatagridView
        private void xianshi_DatagridView()
        {
            #region 显示左边DataGridView
            DataTable dt1 = new DataTable();
            comStr = "select ckdh,SQRQ,CASE sqtj WHEN 0 THEN '未提交' when 1 then '已提交' END,ckbz from Yk_cK01 where sqtj=0 and CKPB=0";
            dt1.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null));
            dataGridView1.Rows.Clear();
            if (dt1.Rows.Count > 0)
            {
                dataGridView1.Rows.Add(dt1.Rows.Count);
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    dataGridView1.Rows[i].Cells[0].Value = dt1.Rows[i][0].ToString();
                    dataGridView1.Rows[i].Cells[1].Value = Convert.ToDateTime(dt1.Rows[i][1]).ToString("yyyy-MM-dd").Replace('-', '.');
                    dataGridView1.Rows[i].Cells[2].Value = dt1.Rows[i][2].ToString();
                    dataGridView1.Rows[i].Cells[3].Value = dt1.Rows[i][3].ToString();
                }
            }
	        #endregion

            #region 显示右边DataGridView
            DataTable dt2 = new DataTable();
            comStr = "select ckdh,ckrq,ckbz from Yk_cK01 where sqtj=1 and CKPB=1";
            dt2.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null));
            dataGridView2.Rows.Clear();
            if (dt2.Rows.Count > 0)
            {
                dataGridView2.Rows.Add(dt2.Rows.Count);
                for (int i = 0; i < dt2.Rows.Count; i++)
                {
                    dataGridView2.Rows[i].Cells[0].Value = dt2.Rows[i][0].ToString();
                    dataGridView2.Rows[i].Cells[1].Value = Convert.ToDateTime(dt2.Rows[i][1]).ToString("yyyy-MM-dd").Replace('-', '.');
                    dataGridView2.Rows[i].Cells[2].Value = dt2.Rows[i][2].ToString();
                }
            }
        		 
	        #endregion

            #region datagridView默认不选中单元格，使之成蓝色
            if (dataGridView1.Rows.Count > 0)
            {
                dataGridView1.Rows[0].Selected = false;
            }
            if (dataGridView2.Rows.Count > 0)
            {
                dataGridView2.Rows[0].Selected = false;
            }
            #endregion
        }
        #endregion

        private void gl_ypsl_Load(object sender, EventArgs e)
        {

            //dataGridView1.CurrentCell = null;
            //入库方式
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            DataTable dt = new DataTable();
            if (mainform.yfsb==2)    //2表示中药房
            {
                yksb = 2;
            }
            else
            {
                yksb = 1;
            }
            comStr = "select ykmc from Yk_YKLB where yksb=" + yksb + "";
            string ykmc=(SqlHelper.ExecuteScalar(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null)).ToString();
            comboBox1.Items.Add(ykmc);
            comboBox1.Text = ykmc;

            xianshi_DatagridView();

        }

        #region 增加按钮
        private void button2_Click(object sender, EventArgs e)
        {

        }
        #endregion

        /// <summary>
        /// 选择单元格，弹出领药单信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            //lingyaodanhao = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            //gl_ypsld_zuo ypsld_zuo = new gl_ypsld_zuo();
            //ypsld_zuo.Owner = this;
            //ypsld_zuo.ShowDialog();
        }

        private void dataGridView2_KeyDown(object sender, KeyEventArgs e)
        {
            //lingyaodanhao = dataGridView2.CurrentRow.Cells[0].Value.ToString();
            //gl_ypsld_you ypsld_you = new gl_ypsld_you();
            //ypsld_you.Owner = this;
            //ypsld_you.ShowDialog();
        }

        /// <summary>
        /// datagridView失去焦点，单元格不显示蓝色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_Leave(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {

                dataGridView1.Rows[i].Selected = false;
            }
        }

        private void dataGridView2_Leave(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView2.Rows.Count; i++)
            {

                dataGridView2.Rows[i].Selected = false;
            }
        }


        #region 提交按钮
        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 0)
            {
                if (dataGridView1.CurrentRow.Cells[2].Value.ToString() == "未提交")
                {
                    if (MessageBox.Show("是否提交领药单" + "" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "" + "？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        try
                        {
                            comStr = "update Yk_cK01 set sqtj='1',czgh='" + Program.ygbh + "' where ckdh='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'";
                            SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null);
                            xianshi_DatagridView();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message.ToString());
                        }
                    }
                }
            }
        }
        #endregion

        #region 删除按钮
        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 0)
            {
                if (dataGridView1.CurrentRow.Cells[2].Value.ToString() == "未提交")
                {
                    if (MessageBox.Show("是否删除领药单" + "" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "" + "？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        try
                        {
                            string comStr = "delete from YF_RK01 where rkdh='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'";
                            SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null);
                            xianshi_DatagridView();
                            MessageBox.Show("删除领药单" + "" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "" + "成功！");
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message.ToString());
                        }
                    }
                }
            }
        }
        #endregion

        #region 退出按钮
        private void button6_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region 确定按钮
        private void button5_Click(object sender, EventArgs e)
        {
            //lingyaodanhao = dataGridView2.CurrentRow.Cells[0].Value.ToString();
            //gl_ypsld_you ypsld_you = new gl_ypsld_you();
            //ypsld_you.Owner = this;
            //ypsld_you.ShowDialog();
        }
        #endregion

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            ControlPaint.DrawBorder(e.Graphics,
                              panel1.ClientRectangle,
                              Color.Red,
                              1,
                              ButtonBorderStyle.Dashed,
                              Color.Red,
                              1,
                              ButtonBorderStyle.Dashed,
                              Color.Red,
                              1,
                              ButtonBorderStyle.Dashed,
                              Color.Red,
                              1,
                              ButtonBorderStyle.Dashed); 

            //ControlPaint.DrawBorder(e.Graphics,
            //                   panel1.ClientRectangle,
            //                   Color.Red,
            //                   3,
            //                   ButtonBorderStyle.Solid,
            //                   Color.Red,
            //                   3,
            //                   ButtonBorderStyle.Solid,
            //                   Color.Red,
            //                   3,
            //                   ButtonBorderStyle.Solid,
            //                   Color.Red,
            //                   3,
            //                   ButtonBorderStyle.Solid); 
        }

        //药库确认药房的领药单button
        private void button7_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 0)
            {
                if (dataGridView1.Focus() && dataGridView1.CurrentRow.Cells[2].Value.ToString() == "已提交")
                {
                    if (MessageBox.Show("是否确认药房领药单" + "" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "" + "？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        comStr = "update Yk_cK01 set CKPB='1' where ckdh='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'";
                        SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null);
                        xianshi_DatagridView();
                    }
                }
            }
        }
    }
}
